package Raid;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.opengl.Texture;


public class Parasquid extends Baddy{

	static String[] texNames = {"res/grass_4.png"};
	static Texture[] textures;
	double damageToBlock = 10;
	double floatYSpeed = .02;
	boolean floating = true;
	
	public Parasquid(Environment env, double startx, double starty){
		x = startx;
		y = starty;
		width = 16;
		height = 16;
		maxLife = 100;
		life = 100;
		xSpeed = 0;
		maxXSpeed = 0.03;
		ySpeed = 0;
		maxYSpeed = 0.25;
		gravity = 0.0006;
		facing = -1;
		type = BaddyType.PARASQUID;
		maskWidth = 14;
		maskHeight = 16;
		mask = new Rectangle((float)(x-maskWidth/2), (float)y, (float)(maskWidth), (float)maskHeight);
		standing = false;
		jumping = false;
		jumpSpacing = 1000;
		timeSinceJump = jumpSpacing + 1;
		maxJumps = 1;
		jumps = 0;
		this.env = env;
		texID = 0;
		texture = Grass.textures[6];
	}
	
	
	
	public boolean takeDamage(double damage){
		life -= damage;
		if (life < 0){
			return true;
		}
		return false;
	}
	
	
	public void doLogic(int delta){
		jumping = false;
		if (floating == true){
			floatDown(delta);
		} else
			crawl(delta);
		updatePhysics(delta);
		mask.setLocation((float)(x - maskWidth/2), (float)y);
		
		//texture = textures[texID];
	}
	
	public void fallingPhysics(int delta){
		ySpeed += gravity*delta;
		standing = false;
		Rectangle newMask = getMask(0, (float)ySpeed);
		if (ySpeed > 0){
			Block[] beneathBlocks = env.getBeneathBlocks(x, y, width);
			for (int i = 0; i < 3; i++){
				if (beneathBlocks[i] != null && beneathBlocks[i].solid && beneathBlocks[i].mask.getMinY() - newMask.getMinY() > 8 
						&& Utils.intersects(beneathBlocks[i].mask, newMask)){
					floating = false;
					ySpeed = 0;
					y = beneathBlocks[i].y - width;
					standing = true;
					jumps = 0;
					break;
				}
			}
		} else if (ySpeed < 0){
			Block[] aboveBlocks = env.getAboveBlocks(x, y, width);
			for (int i = 0; i < 6; i++){
				if (aboveBlocks[i] != null && aboveBlocks[i].solid 
						&& Utils.intersects(aboveBlocks[i].mask, newMask)){
					ySpeed = 0;
					y = aboveBlocks[i].y + Block.width;
					break;
				}
			}
		}
	}
	
	
	public void horizontalPhysics(int delta){
		Block[] sideBlocks = env.getSideBlocks(x, y, width);
		Rectangle newMask = getMask((float)xSpeed, 0f);
		if (xSpeed > 0){
			for (int i = 1; i < 6; i+=2){
				if (sideBlocks[i] != null && sideBlocks[i].solid && Utils.intersects(sideBlocks[i].mask, newMask)){
					xSpeed = 0;
					facing = -1;
					x = sideBlocks[i].x - mask.getWidth()/2.0f;
					env.damageBlock(sideBlocks[i].x, sideBlocks[i].y, damageToBlock);
					break;
				}
			}
		} else if (xSpeed < 0) {
			for (int i = 0; i < 6; i+=2){
				if (sideBlocks[i] != null && sideBlocks[i].solid && Utils.intersects(sideBlocks[i].mask, newMask)){
					xSpeed = 0;
					facing = 1;
					x = sideBlocks[i].x + Block.width + mask.getWidth()/2.0f;
					env.damageBlock(sideBlocks[i].x, sideBlocks[i].y, damageToBlock);
					break;
				}
			}
		}
	}
	
	public void floatDown(int delta){
		ySpeed = this.floatYSpeed;
	}
	
	
	public void crawl(int delta){
		if (x >= Raid.windowWidth) {
			facing = -1;
		} else if (x <= 0){
			facing = 1;
		}
		xSpeed = facing*maxXSpeed;
	}
	
	
	
	public void render(){
		//texture = textures[texID];
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		texture.bind();
		//GL11.glColor3d((life/2+50)/100.0, (life/2+50)/100.0, (life/2+50)/100.0); // Could make this faster!!
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glNormal3d(0, 0, 1);
			GL11.glTexCoord2d(0.0, 0.0);
			GL11.glVertex3i((int)(x-width/2), (int)y, 0);
			GL11.glTexCoord2d(1.0, 0.0);
			GL11.glVertex3i((int)(x+width/2), (int)y, 0);
			GL11.glTexCoord2d(1.0, 1.0);
			GL11.glVertex3i((int)(x+width/2), (int)(y+width), 0);
			GL11.glTexCoord2d(0.0, 1.0);
			GL11.glVertex3d((int)(x-width/2), (int)(y+width), 0);
		GL11.glEnd();
	}
	
	
}
